package com.ysh.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ysh.ssm.dto.R;
import com.ysh.ssm.mapper.UserMapper;
import com.ysh.ssm.pojo.TbUser;
import com.ysh.ssm.service.UserService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 袁胜虎 on 2022/8/31 21:58
 */
@RestController
@RequestMapping("/users")
public class UserController {
    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    @GetMapping("/user/{id}")
    public R findById(@PathVariable Long id){
        return R.OK(userService.findById(id));
    }

    @PostMapping("/user")
    public R adduser(@RequestBody TbUser tbUser){
        userService.addUser(tbUser);
        return R.OK();
    }
    @GetMapping("/users")
    public R findUserByPage(@RequestParam(defaultValue = "2")int pageSize,@RequestParam(defaultValue = "1")int pageNum){
        PageInfo<TbUser> pageInfo = userService.findByPage(pageSize,pageNum);
        return R.OK(pageInfo);

    }
    @GetMapping("/user/{username}/{password}")
    public R findNameAndPassword(@PathVariable String username, @PathVariable String password){
        System.err.println(username);
        System.err.println(password);
        return R.OK(userService.findUsernameAndPassword(username,password));
    }
    @DeleteMapping("/user/{id}")
    public R deleteById(@PathVariable Long id){
        userService.deleteById(id);
        return R.OK();
    }
    @PutMapping("/user/{username}/{password}/{email}/{gender}/{id}")
    public R updateUser(@PathVariable String username,@PathVariable String password,@PathVariable String email,@PathVariable String gender,@PathVariable Long id){
        userService.updateUser(username,password,email,gender,id);
        return R.OK();
    }
}
